<?php
namespace Common\Model; 
use Common\Service\WeixinTemplateService; 
use Common\Model\ProjectAppointmentModel;
/**
* 微信模板用户
* @date: 2018年3月19日 上午10:42:21
* @author: liuyang
*/
class WeixinTemplateTaskModel  extends BaseModel { 
    
   const STATUS_NO=0;
   const STATUS_YES=1;
   const STATUS_FAIL=-1;
   
 
    /**
     * 添加模板任务
     * @param  $weixinTemplateId
     * @param  $openid
     * @param  $templateData
     * @param  $url
     * @param  $sendDate
     * @return \Think\mixed
     */
   public function addTask($weixinTemplateId, $openid, $templateData,$url,$sendDate){
       if($openid != null && strlen($openid)>5){
           $data = array();
           $data['weixin_template_id']=$weixinTemplateId;
           $data['openid']=$openid;
           $data['data']=json_encode($templateData);
           $data['createdate']=time();
           $data['url']=$url;
           $data['senddate']=$sendDate !=null ? $sendDate : date("Y-m-d H:i:s",time()) ;
           $data['createdate']=time();
           return $this->add($data);
       }else{
           return false;
       }
   }
   
   private function _getIdsBySendDateAndOpenId($openId,$sendDate){
       $where = array();
       $where['openid']=$openId;
       $where['senddate']=$sendDate;
       $list = $this->where($where)->select();
       $ids = array();
       foreach($list as $vo){
           $ids[]= $vo['id'];
       }
       return $ids;
   }
   /**
    * 定时任务
    * @return boolean
    */
   public function send(){
       $where = array();
       $where['status']=self::STATUS_NO;
       $obj = $this->where($where)->order("senddate ASC")->find();
       $now = time(); 
       if($obj['id']>0 && strtotime($obj['senddate']) <= $now){
            $data = json_decode($obj['data'],true);
            $ids = array($obj['id']);
            $isSend = true;
            //咨询消息处理
            if($obj['weixin_template_id']==WeixinTemplateModel::SERVICE_ID){
                //查询同一时间给同一个经纪人的消息合并推送，并且查看是否处理完
                $ids = $this->_getIdsBySendDateAndOpenId($obj['openid'], $obj['senddate']);
                $data['first'] = count($ids);
                $weixinObj = (new WeixinMemberModel())->getByOpenId($obj['openid']);
                if($weixinObj['id']>0){
                   $total =  (new ProjectAppointmentModel())->getTotalBySalesId($weixinObj['user_id']);
                   if($total < 1){
                       $isSend = false;
                   }
                }
            }  
            if($isSend){
                $rstVal = WeixinTemplateService::send($obj['weixin_template_id'], $obj['openid'], $data, $obj['url']);
            }
            $data = array();
            $where = array();
            $where['id']=array("IN",$ids);
            if($rstVal['errcode']==0){
                $data['status']=self::STATUS_YES;
            }else{
                $data['status']=self::STATUS_FAIL;
            }
            $this->where($where)->save($data);
            return false;
       }else{
            return true;
       }
   }
    
}
	 